﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Wjtour.Common;

namespace Wjtour.BLL
{
    public class scene_bll : base_dll
    {
        public List<Wjtour.Domain.scene> GetList(int top)
        {
            string sql = string.Format("select top {0} * from scene order by isrecommend desc, id desc", top);
            return GetAll<Wjtour.Domain.scene>(sql).ToList();
        }

        public PaginationData<Wjtour.Domain.scene> GetList(string q, string type, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and scenename like '%" + Utility.GetSafeSql(q) + "%'";
            if (!string.IsNullOrEmpty(type))
                where += " and scenetype in ( " + type + ")";
            string sql = string.Format(@"select * from scene 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.scene> data = GetPaginationData<Wjtour.Domain.scene>(sql, page, pagesize);
            return data;
        }

        public PaginationData<Wjtour.Domain.scene> GetList(string q, int page, int pagesize = 20)
        {
            string where = " where 1=1";
            if (!string.IsNullOrEmpty(q))
                where += " and scenename like '%" + Utility.GetSafeSql(q) + "%'";
            string sql = string.Format(@"select * from scene 
                {0} 
                order by id desc", where);
            PaginationData<Wjtour.Domain.scene> data = GetPaginationData<Wjtour.Domain.scene>(sql, page, pagesize);
            return data;
        }

        public Wjtour.Domain.scene Load(int id)
        {
            string sql = "select * from scene where id=" + id;
            return DbAccess.GetObjects<Wjtour.Domain.scene>(sql).FirstOrDefault();
        }

        public bool Save(Wjtour.Domain.scene entity)
        {
            string sql = "";

            DbCommand cmd = DbAccess.CreateCommand();

            if (entity.id > 0)
            {
                sql = @"UPDATE scene SET 
                scenename = @scenename,
                address = @address,
                scenerank = @scenerank,
                opentime = @opentime,
                contactphone = @contactphone,
                sceneimpression = @sceneimpression,
                sceneweb = @sceneweb,
                price = @price,
                scenetype = @scenetype,
                trafficguide = @trafficguide,
                introduce = @introduce,
                img = @img,
                addtime = @addtime,
                isrecommend = @isrecommend
                 WHERE id=@id";
                DbAccess.AddInParameter(cmd, "@id", DbType.Int32, entity.id);

            }
            else
            {
                sql = @"INSERT INTO scene( 
                scenename,address,scenerank,opentime,contactphone,sceneimpression,sceneweb,price,scenetype,trafficguide,introduce,img,addtime,isrecommend)VALUES(
                @scenename,@address,@scenerank,@opentime,@contactphone,@sceneimpression,@sceneweb,@price,@scenetype,@trafficguide,@introduce,@img,@addtime,@isrecommend)";
            }
            DbAccess.AddInParameter(cmd, "@scenename", DbType.String, entity.scenename);
            DbAccess.AddInParameter(cmd, "@address", DbType.String, entity.address);
            DbAccess.AddInParameter(cmd, "@scenerank", DbType.Int32, entity.scenerank);
            DbAccess.AddInParameter(cmd, "@opentime", DbType.String, entity.opentime);
            DbAccess.AddInParameter(cmd, "@contactphone", DbType.String, entity.contactphone);
            DbAccess.AddInParameter(cmd, "@sceneimpression", DbType.String, entity.sceneimpression);
            DbAccess.AddInParameter(cmd, "@sceneweb", DbType.String, entity.sceneweb);
            DbAccess.AddInParameter(cmd, "@price", DbType.Decimal, entity.price);
            DbAccess.AddInParameter(cmd, "@scenetype", DbType.Int32, entity.scenetype);
            DbAccess.AddInParameter(cmd, "@trafficguide", DbType.String, entity.trafficguide);
            DbAccess.AddInParameter(cmd, "@introduce", DbType.String, entity.introduce);
            DbAccess.AddInParameter(cmd, "@img", DbType.String, entity.img);
            DbAccess.AddInParameter(cmd, "@addtime", DbType.DateTime, entity.addtime);
            DbAccess.AddInParameter(cmd, "@isrecommend", DbType.Int32, entity.isrecommend);
            cmd.CommandText = sql;
            cmd.CommandType = System.Data.CommandType.Text;
            return DbAccess.ExecuteNonQuery(cmd) > 0;

        }


        public int BatchDelete(string ids)
        {
            string sql = "delete from scene where id in(" + ids + ")";
            return DbAccess.ExecuteNonQuery(sql);
        }
    }
}
